﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LaserLineDetector.ViewModel;
using LaserLineDetector.View;
using CheckPermit;

namespace LaserLineDetector
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : DMSkin.DMSkinWindow
    {

        /// <summary>
        /// ViewModel
        /// </summary>
        public Vm_MainWindow ViewModel
        {
            get { return (Vm_MainWindow)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ViewModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(Vm_MainWindow), 
                typeof(MainWindow), new PropertyMetadata(new Vm_MainWindow()));


        /// <summary>
        /// ctor
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗口关闭事件响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _this_Closed(object sender, EventArgs e)
        {
            if (ViewModel.Dispatch != null)
            {
                ViewModel.Dispatch.Command_Detach.Execute(null);
            }

            //关闭日志窗口
            winLogList.IsQuit = true;
            winLogList.Close();
        }

        /// <summary>
        /// 窗口加载事件响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _this_Loaded(object sender, RoutedEventArgs e)
        {
            //获取版本号
            ViewModel.Version = Application.ResourceAssembly.GetName().Version.ToString();
        }

        /// <summary>
        /// 打开日志记录窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenLogList_Click(object sender, RoutedEventArgs e)
        {
            winLogList.Show();
        }

        #region Private

        /// <summary>
        /// 日志窗口
        /// </summary>
        private WinLogList winLogList = new WinLogList();

        #endregion

        /// <summary>
        /// 保存截图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShotScr_Click(object sender, RoutedEventArgs e)
        {
            //停止测量线程
            ViewModel.Dispatch.Command_Stop.Execute(null);

            //截图
            System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap((int)ActualWidth, (int)ActualHeight);
            System.Drawing.Size s = new System.Drawing.Size(memoryImage.Width, memoryImage.Height);
            Point pointUL = PointToScreen(new Point(0, 0));
            System.Drawing.Graphics memoryGraphics = System.Drawing.Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen((int)pointUL.X, (int)pointUL.Y, 0, 0, s);

            //选择保存路径
            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
            sfd.FileName = "未命名";
            sfd.DefaultExt = ".png";
            sfd.Filter = "PNG图片(*.png)|*.png";
            if (sfd.ShowDialog() == true)
            {
                memoryImage.Save(sfd.FileName);
            }
        }
    }
}
